//
//  XCBaseLabel.m
//  MVVM-ViewModel-NetWork-Model
//
//  Created by 刘小椿 on 16/11/14.
//  Copyright © 2016年 刘小椿. All rights reserved.
//

#import "XCBaseLabel.h"

@implementation XCBaseLabel
 
//
- (void)setCornerRadius:(CGFloat)cornerRadius
{
    _cornerRadius = cornerRadius;
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = cornerRadius > 0 ? YES : NO;
    self.layer.shouldRasterize = YES; // 设置光栅化，可以使离屏渲染的结果缓存到内存中存为位图，使用的时候直接使用缓存，节省了一直离屏渲染损耗的性能。
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;  //UIImageView不加这句会产生一点模糊

}

- (void)setBorderColor:(UIColor *)borderColor
{
    _borderColor = [borderColor copy];
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth
{
    _borderWidth = borderWidth;
    self.layer.borderWidth = borderWidth;
}


@synthesize verticalAlignment = verticalAlignment_;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.verticalAlignment = VerticalAlignmentMiddle;
    }
    return self;
}

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    verticalAlignment_ = verticalAlignment;
    [self setNeedsLayout];
}


- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    switch (self.verticalAlignment) {
        case VerticalAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case VerticalAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case VerticalAlignmentPadding:
            textRect.origin.x -= self.contentInset.left;
            textRect.origin.y -= self.contentInset.top;
            textRect.size.width += self.contentInset.left + self.contentInset.right;
            textRect.size.height += self.contentInset.top + self.contentInset.bottom;
            break;
        case VerticalAlignmentMiddle:
            // Fall through.
        default:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
    }
    return textRect;
}

-(void)drawTextInRect:(CGRect)requestedRect {
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
     [super drawTextInRect:UIEdgeInsetsInsetRect(actualRect, self.contentInset)];
//    [super drawTextInRect:actualRect];
}

- (void)setContentInset:(UIEdgeInsets)contentInset {
    _contentInset = contentInset;
    NSString *tempString = self.text;
    self.text = @"";
    self.text = tempString;
//    [self setNeedsDisplay];
}

 

@end
